package com.tsu.mall.admin.controller;

import com.tsu.mall.admin.service.IAttendantService;
import com.tsu.mall.admin.util.RequestUtil;
import com.tsu.mall.common.VO.LayuiTableVO;
import com.tsu.mall.common.util.StringDbutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/")
public class LoginController {
    @Autowired
    private IAttendantService service;
    @GetMapping("/login")
    public String page() {
        return "login";
    }

    @GetMapping("/psd")
    public String psd() {
        return "psd";
    }

    @GetMapping("/register")
    public String register() {
        return "register";
    }

    @GetMapping("/success")
    public String success() {
        return "success";
    }

    @PostMapping ("/login")
    @ResponseBody
    public LayuiTableVO check(String username, String password){
        LayuiTableVO vo=new LayuiTableVO();
        HttpServletRequest request= RequestUtil.getRequest();
        //对密码进行加密之后，再与数据库中数据进行比较
         password= StringDbutil.md5(password);
        //获取前台传来请求中的验证码，判断与session中是否一致
        String captcha=request.getParameter("captcha");
        if(!(request.getSession().getAttribute("captcha")).equals(captcha)) {
            vo.setCode(1);
            vo.setMsg("验证码错误!");
            vo.setData(new String("/login"));
            return vo;
        }else {

        }
        Boolean check=service.check(username,password);
        Object msg=RequestUtil.getSession().getAttribute("msg");
        if (msg != null) {
            vo.setCode(1);
            vo.setMsg((String) msg);
        }else {
            if(check){
                vo.setData(new String("/index"));
                return vo;
            }else{
                vo.setCode(1);
                vo.setMsg("用户名或者密码错误!");
                vo.setData(new String("/login"));
            }
        }
        return vo;
    }

}
